package com.example.animations.activities;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.animations.R;
import com.example.animations.controls.PickerView;
import com.example.animations.controls.SliderView;

public class TextAnimationActivity extends AppCompatActivity implements TextWatcher {
    private final float minDuration = 0.1f;
    private final float maxDuration = 2.0f;
    private TextView _animatableText;
    private ValueAnimator _valueAnimator;
    private boolean _isLarge = true;
    private int _minSize = 50;
    private int _maxSize = 100;
    private float _duration = 1.0f;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_animation);

        _animatableText = findViewById(R.id.animatable_text);
        _animatableText.setTextSize(_isLarge ? _maxSize : _minSize);

        SliderView durationSlider = findViewById(R.id.duration_slider);
        durationSlider.setTitle("Duration");
        durationSlider.setMinMaxValues(minDuration, maxDuration);
        durationSlider.setValue(_duration);
        durationSlider.onValueChanged(this::onDurationChanged);

        PickerView minSizePicker = findViewById(R.id.min_value_picker);
        minSizePicker.setMinMaxValues(10, 50);
        minSizePicker.setValue(_minSize);
        minSizePicker.setTitle("From Size");
        minSizePicker.setOnValueChangedListener(this::updateMinSize);

        PickerView maxSizePicker = findViewById(R.id.max_value_picker);
        maxSizePicker.setMinMaxValues(51, 100);
        maxSizePicker.setValue(_maxSize);
        maxSizePicker.setTitle("To Size");
        maxSizePicker.setOnValueChangedListener(this::updateMaxSize);

        ((Button) findViewById(R.id.play_button)).setOnClickListener(view -> startAnimation());
        ((EditText) findViewById(R.id.edit_text)).addTextChangedListener(this);

        _valueAnimator = ValueAnimator.ofFloat(_minSize, _maxSize);
        _valueAnimator.addUpdateListener(this::onAnimate);
        updateAnimationDuration();
    }

    private void onDurationChanged(float value) {
        _duration = value;
        updateAnimationDuration();
    }

    private void updateAnimationDuration() {
        _valueAnimator.setDuration(Math.round(_duration * 1000));
    }

    private void onAnimate(ValueAnimator valueAnimator) {
        float animatedValue = (float) valueAnimator.getAnimatedValue();
        _animatableText.setTextSize(animatedValue);
    }

    private void startAnimation() {
        _valueAnimator.setFloatValues(_minSize, _maxSize);
        if (_isLarge) {
            _valueAnimator.reverse();
            _isLarge = false;
        } else {
            _valueAnimator.start();
            _isLarge = true;
        }
    }

    private void updateMinSize(int size) {
        _minSize = size;
        if (!_isLarge && !_valueAnimator.isRunning()) {
            _animatableText.setTextSize(_minSize);
        }
    }

    private void updateMaxSize(int size) {
        _maxSize = size;
        if (_isLarge && !_valueAnimator.isRunning()) {
            _animatableText.setTextSize(_maxSize);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        _animatableText.setText(String.valueOf(charSequence));
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }
}
